layui.define(['layer'], function (exports) {
    var MOD_NAME = 'quesLogic',
		$ = layui.jquery;

    var quesLogic = function() {
        this.logicItemList = [];
        this.id_logics = {};
        this.cur_id;    //当前逻辑处理题id
        this.cur_index; //当前逻辑处理题号
        this.dataList;
    };

    quesLogic.prototype.init = function(list) {
        this.dataList = list;
    };

    var executeOp = { //v1:输入值，v2:条件值
        eq: function eq(v1, v2) {
            if (!v1) {
                return false;
            }
            // 当type=CHECK_BOX时此处应为包含关系
            return Array.isArray(v1) ? v1.includes(v2) : v1 === v2;
        },
        ne: function ne(v1, v2) {
            if (!Number(v1)) {
            return false;
            }
            return Array.isArray(v1) ? !v1.includes(v2) : v1 !== v2;
        },
        is: function is(v1, v2) {
            if (!v1) {
                return false;
            }
            return v1 === v2;
        },
        gt: function gt(v1, v2) {
            // 是否是数字
            if (!Number(v1) || !Number(v2)) {
            return false;
            }
            return v1 > v2;
        },
        ge: function ge(v1, v2) {
            // 是否是数字
            if (!Number(v1) || !Number(v2)) {
            return false;
            }
            return v1 >= v2;
        },
        lt: function lt(v1, v2) {
            // 是否是数字
            if (!Number(v1) || !Number(v2)) {
            return false;
            }
            return v1 < v2;
        },
        le: function le(v1, v2) {
            // 是否是数字
            if (!Number(v1) || !Number(v2)) {
            return false;
            }
            return v1 <= v2;
        }
    };

    quesLogic.prototype.saveLogicItems = function(items) {
        console.log(items);
        var that = this;
        /*
        logicItem = 
        {   
            conditions: [
                {block_id, op, value, status}
            ],
            relation: 'and/or',
            events: [
                {action, block_id}
            ]
            //conditions中的条件均满足时，触发events中所有事件
        }
        */
        // var logicItem = {conditions: conditions, relation: relation, events: triggers};
        for (const key in that.id_logics) {
            that.id_logics[key].splice(0,that.id_logics[key].length);
        }
        that.logicItemList.splice(0,that.logicItemList.length);
        for (let i = 0; i < items.length; i++) {
            var _new = JSON.parse(JSON.stringify(items[i]));
            that.logicItemList.push(_new);
            for (let j = 0; j < _new.conditions.length; j++) {
                var condition = _new.conditions[j];
                if(!Array.isArray(that.id_logics[condition.block_id])) {
                    that.id_logics[condition.block_id] = [];
                }
                
                that.id_logics[condition.block_id].push(_new);
            }
        }
        console.log('######SAVE');
        console.log(that.logicItemList);
        console.log(that.id_logics);
        console.log('SAVE######');
    };

    quesLogic.prototype.handleLogic = function(index, id, value) { //问卷初始化、表单项值改变时调用（题号，id，值）
        console.log(index, id, value);
        var that = this;
        var logics = that.id_logics[id];
        that.cur_id = id;
        that.cur_index = index;

        var true_event = [];
        var false_event = [];
        for (let key in logics) {
            var logic = logics[key];
            var flag = logic.relation === 'and' ? true : false;
            for (let i = 0; i < logic.conditions.length; i++) { //检查是否所有条件成立：当前题目相关的条件checkCondition，其他条件仅需检查status
                var condition = logic.conditions[i];
                if(logic.relation === 'and') {
                    if(condition.block_id == id) {
                        if(!that.checkCondition(value, condition)) {
                            flag = false;
                            break;
                        }
                    }
                    else {
                        if(!condition.status) {
                            flag = false;
                        }
                    }
                }
                else if(logic.relation === 'or'){
                    if(condition.block_id == id) {
                        if(that.checkCondition(value, condition)) {
                            flag = true;
                            break;
                        }
                    }
                    else {
                        if(condition.status) {
                            flag = true;
                        }
                    }
                }
                else {
                    flag = that.checkCondition(value, condition);
                }
            }
            if(flag) { //如果所有条件成立，执行所有事件
                for (let i = 0; i < logic.events.length; i++) {
                    true_event.push(logic.events[i]);
                }
            }
            else {
                for (let i = 0; i < logic.events.length; i++) {
                    false_event.push(logic.events[i]);
                }
            }
        }
        //先回溯失效事件，在执行生效事件
        for (let i = 0; i < false_event.length; i++) {
            that.cancelEvent(false_event[i]);
        }
        for (let i = 0; i < true_event.length; i++) {
            that.executeEvent(true_event[i]);
        }
    };
    quesLogic.prototype.checkCondition = function(input, condition) { //Called by handleLogic()
        if(executeOp[condition.op](input, condition.value)) {
            condition.status = true;
        }
        else {
            condition.status = false;
        }
        return condition.status
    };
    quesLogic.prototype.executeEvent = function(event) { //Called by handleLogic()
        var that = this;
        var action = event.action;
        var id = event.block_id;
        if(action === 'show') {
            $(`div[data-id="${id}"]`)[1].style.display = 'block';
        }
        else if(action === 'hide') {
            console.log('hide',$(`div[data-id="${id}"]`)[1]);
            $(`div[data-id="${id}"]`)[1].style.display = 'none';
            console.log($(`div[data-id="${id}"]`)[1].style.display);
        }
        else if(action === 'goto') {
            for (let i = that.cur_index; i < that.dataList.length; i++) {
                if(that.dataList[i].id === id) break;
				$(`div[data-id="${that.dataList[i].id}"]`)[1].style.display = 'none';
			}
        }
    };
    quesLogic.prototype.cancelEvent = function(event) { //Called by handleLogic()
        var that = this;
        var action = event.action;
        var id = event.block_id;
        if(action === 'show') {
            $(`div[data-id="${id}"]`)[1].style.display = 'none';
        }
        else if(action === 'hide') {
            $(`div[data-id="${id}"]`)[1].style.display = 'block';
        }
        else if(action === 'goto') {
            for (let i = that.cur_index; i < that.dataList.length; i++) {
                if(that.dataList[i].id === id) break;
				$(`div[data-id="${that.dataList[i].id}"]`)[1].style.display = 'block';
			}
        }
    };
    

    exports(MOD_NAME, new quesLogic());
});